package tyris.util.command;

import ij.ImagePlus;
import tyris.ui.AdrustImage;
import tyris.ui.MainFrame;
import tyris.util.ByteUtils;

public class RotateCommand implements Command {
	/**
	 * rotate direction: left
	 */
	public static int ROTATE_LEFT = 0;

	/**
	 * rotate direction: right
	 */
	public static int ROTATE_RIGHT = 1;

	/**
	 * rotate direction
	 */
	private int direction;

	public RotateCommand(int direction) {
		this.direction = direction;
	}

	@Override
	public boolean execute() {
		ImagePlus img = MainFrame.getActiveImp();
		if (ROTATE_LEFT == direction) {
			img.setProcessor(img.getProcessor().rotateLeft());
			if (MainFrame.activeImp.getProcessor().getPixels().getClass().toString().equals("class [B")) {
				AdrustImage.oriPixel = ByteUtils.toShortArray((byte[]) MainFrame.activeImp.getProcessor().getPixels());
			} else {
				AdrustImage.oriPixel = (short[]) MainFrame.activeImp.getProcessor().getPixels();
			}
			img.getCanvas().zoomToFullWin();
		} else {
			img.setProcessor(img.getProcessor().rotateRight());
			if (MainFrame.activeImp.getProcessor().getPixels().getClass().toString().equals("class [B")) {
				AdrustImage.oriPixel = ByteUtils.toShortArray((byte[]) MainFrame.activeImp.getProcessor().getPixels());
			} else {
				AdrustImage.oriPixel = (short[]) MainFrame.activeImp.getProcessor().getPixels();
			}
			img.getCanvas().zoomToFullWin();
		}
		return true;
	}

	@Override
	public boolean undo() {
		ImagePlus img = MainFrame.getActiveImp();
		if (ROTATE_RIGHT == direction) {
			img.setProcessor(img.getProcessor().rotateLeft());
			if (MainFrame.activeImp.getProcessor().getPixels().getClass().toString().equals("class [B")) {
				AdrustImage.oriPixel = ByteUtils.toShortArray((byte[]) MainFrame.activeImp.getProcessor().getPixels());
			} else {
				AdrustImage.oriPixel = (short[]) MainFrame.activeImp.getProcessor().getPixels();
			}
			img.getCanvas().zoomToFullWin();
		} else {
			img.setProcessor(img.getProcessor().rotateRight());
			if (MainFrame.activeImp.getProcessor().getPixels().getClass().toString().equals("class [B")) {
				AdrustImage.oriPixel = ByteUtils.toShortArray((byte[]) MainFrame.activeImp.getProcessor().getPixels());
			} else {
				AdrustImage.oriPixel = (short[]) MainFrame.activeImp.getProcessor().getPixels();
			}
			img.getCanvas().zoomToFullWin();
		}
		return true;
	}

}

